import 'dart:math';

import 'package:camerawesome/camerawesome_plugin.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_learn/app/modules/weichat_post/entity/index.dart';
import 'package:getx_learn/app/modules/weichat_post/modules/post/constant/config.dart';
import 'package:getx_learn/app/modules/weichat_post/util/du_picker.dart';
import 'package:getx_learn/app/modules/weichat_post/widgets/camera/camera.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

// 发布类型
enum PostType { image, video }

class PostController extends GetxController {
  // 发布类型
  PostType? postType;

  // 已选中图片列表
  // List<AssetEntity> selectedAssets = [];
  var selectedAssets = <AssetEntity>[].obs;

  // 是否开始拖拽
  var isDragNow = false.obs;

  // 是否将要删除
  var isWillRemove = false.obs;

  // 是否将要排序
  var isWillOrder = false.obs;

  // 被拖拽到的 target id
  var targetId = "";

  // 内容输入控制器
  TextEditingController contentController = TextEditingController();

  // 输入框内容
  final contentText = "".obs;

  // 菜单列表
  final menus = <MenuItemModel>[].obs;

  @override
  void onInit() {
    super.onInit();
    postType = Get.arguments['postType'];
    final result = Get.arguments['selectedAssets'];
    if (result is List<AssetEntity>) {
      selectedAssets.value = result ?? [];
    } else {
      selectedAssets.value = [];
      if (result != null) {
        selectedAssets.value = [result];
      }
    }
    menus.value = [
      MenuItemModel(
        icon: Icons.location_on_outlined,
        title: "所在位置",
      ),
      MenuItemModel(
        icon: Icons.alternate_email_outlined,
        title: "提醒谁看",
      ),
      MenuItemModel(
        icon: Icons.person_outline,
        title: "谁可以看",
        right: "公开"
      ),
    ];
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    contentController.dispose();
    super.onClose();
  }

  /// 选择图片
  /// @param context 上下文
  void selectPics(BuildContext context) async {
    AssetPickerConfig pickerConfig = AssetPickerConfig(
      selectedAssets: selectedAssets,
      maxAssets: maxAssets,
    );

    final List<AssetEntity>? result =
        await AssetPicker.pickAssets(context, pickerConfig: pickerConfig);
    if (result == null) {
      return;
    }
    selectedAssets.value = result;
    print(result);
  }

  /// 拍摄照片
  /// @param context 上下文
  void taskePhoto(BuildContext context) async {
    // 拍摄照片

    // wechat_camera_picker 库的使用
    // final AssetEntity? result = await CameraPicker.pickFromCamera(
    //   context,
    //   pickerConfig: const CameraPickerConfig(
    //     // 选择器是否可以录像
    //     enableRecording: true,
    //     // 选择器是否需要录制音频。只在 enableRecording 为 true 时有效。
    //     enableAudio: true,
    //     // 选择器是否仅可以录像。只在 enableRecording 为 true 时有效。
    //     onlyEnableRecording: true,
    //   ),
    // );
    // if (result == null) {
    //   return;
    // }
    // selectedAssets.add(result);

    // camerawesome 库的使用
    final AssetEntity? result = await Dupicker.taskVideo(context);
    if (result == null) {
      return;
    }
    postType = PostType.image;
    selectedAssets.clear();
    selectedAssets.add(result);
  }
}
